#include "RoomMgr.hpp"
#include "CSProto.hpp"
#include "Time.hpp"
#include "world/RoomBase.hpp"
#include <cstdint>
#include <iostream>
#include <sys/types.h>
static uint64_t g_room_uid = 0;

RoomBase* RoomMgr::CreateRoom(ROOM_TYPE type)
{
    RoomBase* base = nullptr;
    // create room uid
    uint32_t curTime = getMSTime();
    uint64_t roomUid = ((uint64_t)curTime << 32) | g_room_uid++;
    switch(type)
    {
        case ROOM_TYPE::ROOM_TYPE_PVP3:
            base = new RoomPVP3(roomUid);
            break;
        case ROOM_TYPE::ROOM_TYPE_PVP2:
            base = new RoomPVP2(roomUid);
            break;
        default:
            std::cout << "unkown ROOM_TYPE:" << static_cast<uint32_t>(type);
    }

    // auto add to mgr
    if(base)
    {
        _rooms[roomUid] = base;
    }
    return base;
}

int RoomMgr::PlayerCreateRoom(std::shared_ptr<Player> player, const cs::CreateRoomReq& msg)
{
    int errorCode = 0;
    RoomBase* base = CreateRoom(static_cast<ROOM_TYPE>(msg.room_type()));
    if(base == nullptr)
    {
        errorCode = -1;
    }
    return 0;
}